package com.c.ctgproject.adapter.guang

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapter
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.entity.guang.PartOutListEntity
import com.c.ctgproject.ui.collect.BatteryCanHandlerDetailActivity
import com.c.ctgproject.ui.collect.PartHandlerListModelView
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.noDataHandler

class PartCanHandlerAdapter(private val context: Context, private val mList: List<PartOutListEntity.DataBean.ListBean>,private val mViewModel: PartHandlerListModelView?,private val listener:OnCheckBoxClickedListener?=null) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_batter_can_handler_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mCheckBox.isChecked=data.isSelect
            holder.mNameTV.text = data.partsName.noDataHandler()
            holder.mCodeTV.text = data.partsCode.noDataHandler()
            holder.mPartsNumberTV.text = data.partsCount.noDataHandler()
            holder.countUnit.text = data.countUnit.handlerNull()
            holder.weight.text = data.weight.noDataHandler()
            holder.createPersonName.text = data.createPersonName.noDataHandler()
            holder.createTime.text = data.createTime.noDataHandler()
            val enterMode = when (data.partsInMethod) {
                1 -> "拆解"
                2 -> "散件"
                else -> "暂无数据"
            }
            holder.inType.text = enterMode
            holder.oe.text = data.oe.noDataHandler()

//            holder.mParentLL.setOnClickListener {
//                val intent = Intent(context, BatteryCanHandlerDetailActivity::class.java)
//                intent.putExtra(MyConstants.DATA, getGson().toJson(data))
//                intent.putExtra(MyConstants.ID, data.pbId)
//                context.startActivity(intent)
//            }
            holder.mCheckBox.setOnClickListener {
                data.isSelect = holder.mCheckBox.isChecked
                if(holder.mCheckBox.isChecked){
                    mViewModel?.addCarId(data.orderId)
                }else{
                    mViewModel?.removeCarId(data.orderId)
                }
                notifyItemChanged(position)
            }
            holder.mParentLL.setOnClickListener{
                listener?.OnItemCheckBoxClicked(position,data)
            }


            //TODO 不知道会不会出问题
            holder.mCheckBox.visibility=if (mViewModel?.searchMine?.value==true) View.GONE else View.VISIBLE


        }
    }
    interface OnCheckBoxClickedListener{
        fun OnItemCheckBoxClicked(position:Int,data:PartOutListEntity.DataBean.ListBean)
    }
    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val oe: TextView = view.findViewById(R.id.oe)
        val inType: TextView = view.findViewById(R.id.inType)
        val weight: TextView = view.findViewById(R.id.weight)
        val countUnit: TextView = view.findViewById(R.id.countUnit)
        val mNameTV: TextView = view.findViewById(R.id.mNameTV)
        val mCodeTV: TextView = view.findViewById(R.id.mCodeTV)
        val mPartsNumberTV: TextView = view.findViewById(R.id.mPartsNumberTV)
        val createPersonName: TextView = view.findViewById(R.id.createPersonName)
        val createTime: TextView = view.findViewById(R.id.createTime)
        val mCheckBox: CheckBox = view.findViewById(R.id.mCheckBox)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
    }


}